/* communication.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "communication.h"


int sockfd;
FILE * sockf;
int len;
struct sockaddr_in addr;

int communication_init(){
	if ((sockfd=socket(AF_INET, SOCK_STREAM, 0)) < 0){
		perror("socket");
		exit(1);
	}

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	addr.sin_port = htons(9999);
	len = sizeof(addr);

	if (connect(sockfd, (struct sockaddr *)&addr, len) == -1 ) {
		perror("connect");
		exit(1);
	}

	sockf = fdopen(sockfd, "r+");
	return 0;
}

int send_msg(const char *msg){
	write(sockfd, msg, strlen(msg)+1);
	return 0;
}

int recv_msg(char * buf, int max){
	//int nread;
	//nread = read(sockfd, buf, max);
	//buf[nread] = '\0';
	fgets(buf, max, sockf);
	return 0;
}
